<?php
/**
 * CodeIgniter - Controller - Welcome
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Controller - Questions
 *
 * @package		CodeIgniter
 * @subpackage	Controller
 * @category	Controller
 * @author		PHPTest
 * @link		NA
 */
 
class Questions extends Controller 
{
	var $view_array = array();
	var $view_item 	= 'category';	
	var $page_title = 'Online Test - PHP, Mysql, CSS, JavaScript - Questions and Test Category';
	
	// --------------------------------------------------------------------------
	
	/** 
	 * Online Test - Constructor
	 *
	 * @access 		public
	 * @category	function
	 * @param  		NA	 
	 * @return 		void
	 */	
	function Questions()
	{
		parent::Controller();	
		check_user_login();	
	}
	
	// --------------------------------------------------------------------------
	
	/** 
	 * Index
	 *
	 * @access 		public
	 * @category	function
	 * @param  		NA	 
	 * @return 		void
	 */	
	function index()
	{		
		$this->category();
    }	
	
	// --------------------------------------------------------------------------
	
	/** 
	 * Category
	 *
	 * @access 		public
	 * @category	function
	 * @param  		NA	 
	 * @return 		void
	 */	
	function category()
	{	
		$where = array();
		$where['category_status'] 	= 1;
		$query=$this->db->get_where(TABLE_CATEGORY,$where);
		if($query->num_rows()>0)
		{
			// return result set as an associative array			
			$category_data = $query->result_array();
			$this->view_array['category_data'] = $category_data;
		}		
		
		show_page($this->view_item,$this->view_array);
    }
	
	// --------------------------------------------------------------------------
	
	/** 
	 * Category
	 *
	 * @access 		public
	 * @category	function
	 * @param  		integer 
	 * @return 		void
	 */	
	function start_test($cat_id = 0)
	{
		
		$cat_id = (int) $cat_id;
		$where = array();
		$where['category_status'] 	= 1;
		$where['category_id'] 		= $cat_id;		
		$query=$this->db->get_where(TABLE_CATEGORY,$where);
		if($query->num_rows()>0)
		{	
			$category_data = $query->row_array();
			$this->session->set_userdata('questions_category',$category_data);
			redirect('questions/show');
		}
		else
		{
			show_page($this->view_item,$this->view_array);
		}		
	}	
	
	// --------------------------------------------------------------------------
	
	/** 
	 * Check
	 *
	 * @access 		public
	 * @category	function
	 * @param  		NA
	 * @return 		void
	 */	
	function check()
	{
		if(!$_POST)	
			redirect('questions');
		
		$questions_answer = $this->input->post('questions_answer');
		$questions_info = $this->session->userdata('questions_info');

		if(is_array($questions_answer) && count($questions_answer) > 0 && is_array($questions_info) && count($questions_info) > 0)
		{
			$test_ended 	= time();
			$total_time_taken = ($test_ended - $questions_info['test_started']);
			
			$result = array();
			$category_data = $this->session->userdata('questions_category');	
			$where = array();					
			$where['category_id'] 		= $category_data['category_id'];		
			$query=$this->db->get_where(TABLE_QUESTIONS,$where);
			if($query->num_rows()>0)
			{
				$total_qs = $query->num_rows();
				$questions_data = $query->result_array();	
			}
			
			foreach($questions_answer as $q_id => $answer)
			{
				for($i = 0 ; $i < $total_qs; $i++ )
				{
					if($questions_data[$i]['questions_id'] == $q_id)
					{
						if($questions_data[$i]['questions_answer'] == $answer)
							$result[] = 1*$questions_data[$i]['questions_weight'];							
						else
							$result[] = 0;
								
						break;
					}		
				}
			}
			
			$user_data 		= $this->session->userdata('user_data');
			$total_score 	= ((array_sum($result))/$total_qs)*100;			
			$total_time_taken_hrs = date('H:i:s',$total_time_taken);
			$data = array();
			$data['category_id'] 		= $category_data['category_id'];
			$data['user_id'] 			= $user_data['user_id'];
			$data['user_login'] 		= $user_data['user_login'];						
			$data['results_score'] 		= $total_score;
			$data['results_time_taken'] = $total_time_taken;
			$data['results_created'] 	= date('Y:m:d H:i:s');
			
			$this->db->insert(TABLE_RESULTS, $data); 
			
			// unset all question sessions
			$this->session->unset_userdata('questions_category');
			$this->session->unset_userdata('questions_info');		
			
			$success	= 'Congratulations, you have successfully completed the test!!';
			$this->view_item = 'results';
			$this->view_array['user_login'] 		= $user_data['user_login'];
			$this->view_array['user_id'] 			= $user_data['user_id'];
			$this->view_array['success'] 			= $success;
			$this->view_array['total_score'] 		= $total_score;
			$this->view_array['total_time_taken'] 	= $total_time_taken_hrs;
			show_page($this->view_item,$this->view_array);	
		}
		else
		{
			$errors	= '<li>Oops, kindly attempt one or more questions and then submit!! Try again.</li>';
			$this->view_array['errors'] 			= $errors;
			$this->category();
		}	
	}
	
	// --------------------------------------------------------------------------
	
	/** 
	 * Show
	 *
	 * @access 		public
	 * @category	function
	 * @param  		NA
	 * @return 		void
	 */	
	function show()
	{
		$category_data = $this->session->userdata('questions_category');	

		if(isset($category_data['category_id']) && !empty($category_data['category_id']))
		{
			$where 					= array();			
			$where['category_id'] 	= $category_data['category_id'];		
			$query=$this->db->get_where(TABLE_QUESTIONS,$where);
			if($query->num_rows()>0)
			{			
				$questions_data = $query->result_array();	
				$this->view_item = 'questions';
				$questions_info = array();
				$questions_info['total_questions'] 	= $query->num_rows();
				$questions_info['test_started'] 	= time();
				$this->session->set_userdata('questions_info',$questions_info);
				$this->view_array['questions_data'] = $questions_data;
				$this->view_array['category_data'] = $category_data;
			}
			else
			{
				$this->view_array['no_questions'] = true;
			}
			
			show_page($this->view_item,$this->view_array);	
		}
		else
			$this->category();		
	}
}

//EOF Controller Questions
?>